package org.dizem.groupon.model.domain;

import org.dizem.groupon.Constants;
import org.hibernate.annotations.Cascade;

import javax.persistence.*;
import java.util.Date;

/**
 * User: dizem@126.com
 * Time: 11-6-21 上午9:53
 * <p/>
 * 商品实体类
 */
@Entity(name = "t_item")
public class ItemEntity {
	/**
	 * 主键
	 */
	private int id;
	/**
	 * 城市
	 */
	private CityEntity city;
	/**
	 * 标题
	 */
	private String title;
	/**
	 * 名称
	 */
	private String name;
	/**
	 * 分类
	 */
	private CategoryEntity category;
	/**
	 * 市场价
	 */
	private float marketPrice;
	/**
	 * 团购价
	 */
	private float groupPrice;
	/**
	 * 当前购买人数
	 */
	private int currentCount;
	/**
	 * 最低团购数量
	 */
	private int minCount;
	/**
	 * 最大团购数量
	 */
	private int maxCount;
	/**
	 * 开始时间
	 */
	private Date beginTime;
	/**
	 * 结束时间
	 */
	private Date endTime;
	/**
	 * 到达最低人数的时间
	 */
	private Date reachMinCountTime;
	/**
	 * 描述
	 */
	private String description;
	/**
	 * 图片1
	 */
	private ImageEntity image1;
	/**
	 * 图片2
	 */
	private ImageEntity image2;
	/**
	 * 图片3
	 */
	private ImageEntity image3;
	/**
	 * 特色
	 */
	private String feature;
	/**
	 * 商家
	 */
	private MerchantEntity merchant;

	/**
	 * 温馨提示
	 */
	private String tips;
	/**
	 * 特别提示
	 */
	private String specialTips;


	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(targetEntity = CityEntity.class)
	public CityEntity getCity() {
		return city;
	}

	public void setCity(CityEntity city) {
		this.city = city;
	}

	@Column(length = 50, nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column
	public int getCurrentCount() {
		return currentCount;
	}

	public void setCurrentCount(int currentCount) {
		this.currentCount = currentCount;
	}

	@Column(length = 300, nullable = false)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@ManyToOne(targetEntity = CategoryEntity.class)
	public CategoryEntity getCategory() {
		return category;
	}

	public void setCategory(CategoryEntity category) {
		this.category = category;
	}

	@Column(precision = 11, scale = 2, nullable = false)
	public float getMarketPrice() {
		return marketPrice;
	}

	public void setMarketPrice(float marketPrice) {
		this.marketPrice = marketPrice;
	}

	@Column(precision = 11, scale = 2, nullable = false)
	public float getGroupPrice() {
		return groupPrice;
	}

	public void setGroupPrice(float groupPrice) {
		this.groupPrice = groupPrice;
	}

	@Column(nullable = false)
	public int getMinCount() {
		return minCount;
	}

	public void setMinCount(int minCount) {
		this.minCount = minCount;
	}

	@Column
	public int getMaxCount() {
		return maxCount;
	}

	public void setMaxCount(int maxCount) {
		this.maxCount = maxCount;
	}

	@Column(nullable = false)
	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	@Column(nullable = false)
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@Column(length = 700)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToOne(targetEntity = ImageEntity.class)
	public ImageEntity getImage1() {
		return image1;
	}

	public void setImage1(ImageEntity image1) {
		this.image1 = image1;
	}

	@OneToOne(targetEntity = ImageEntity.class)
	@Cascade(org.hibernate.annotations.CascadeType.REMOVE)
	public ImageEntity getImage2() {
		return image2;
	}

	public void setImage2(ImageEntity image2) {
		this.image2 = image2;
	}

	@OneToOne(targetEntity = ImageEntity.class)
	public ImageEntity getImage3() {
		return image3;
	}

	public void setImage3(ImageEntity image3) {
		this.image3 = image3;
	}

	@ManyToOne(targetEntity = MerchantEntity.class)
	public MerchantEntity getMerchant() {
		return merchant;
	}


	public void setMerchant(MerchantEntity merchant) {
		this.merchant = merchant;
	}

	@Column
	public Date getReachMinCountTime() {
		return reachMinCountTime;
	}

	public void setReachMinCountTime(Date reachMinCountTime) {
		this.reachMinCountTime = reachMinCountTime;
	}

	@Column(length = 150)
	public String getFeature() {
		return feature;
	}

	public void setFeature(String feature) {
		this.feature = feature;
	}

	@Column(length = 600)
	public String getTips() {
		return tips;
	}

	public void setTips(String tips) {
		this.tips = tips;
	}

	@Column(length = 600)
	public String getSpecialTips() {
		return specialTips;
	}

	public void setSpecialTips(String specialTips) {
		this.specialTips = specialTips;
	}

	@Override
	public String toString() {
		return "ItemEntity{" +
				"specialTips='" + specialTips + '\'' +
				", id=" + id +
				", city=" + city +
				", title='" + title + '\'' +
				", name='" + name + '\'' +
				", category=" + category +
				", marketPrice=" + marketPrice +
				", groupPrice=" + groupPrice +
				", currentCount=" + currentCount +
				", minCount=" + minCount +
				", maxCount=" + maxCount +
				", beginTime=" + beginTime +
				", endTime=" + endTime +
				", reachMinCountTime=" + reachMinCountTime +
				", description='" + description + '\'' +
				", image1=" + image1 +
				", image2=" + image2 +
				", image3=" + image3 +
				", feature='" + feature + '\'' +
				", merchant=" + merchant +
				", tips='" + tips + '\'' +
				'}';
	}

	@Transient
	public String getTimeState() {
		Date now = new Date();
		if (now.getTime() < beginTime.getTime()) {
			return "团购尚未开始";
		}
		if (now.getTime() > endTime.getTime()) {
			System.out.println("团购尚未结束");
			return "团购已经结束";
		}

		long time = (endTime.getTime() - now.getTime()) / 60000;
		int min = (int) (time % 60);
		int hour = (int) ((time / 60) % 24);
		int day = (int) (time / (60 * 24));
		return String.format("%d天%d小时%d分", day, hour, min);
	}

	@Transient
	public String getFormatBeginTime() {
		return Constants.DATE_FORMAT.format(beginTime);
	}

	@Transient
	public String getFormatEndTime() {
		return Constants.DATE_FORMAT.format(endTime);
	}
}

